import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Jiange
 * @create 2021-05-12-13:22
 */
class Ticket{
    private int number = 30;

    private ReentrantLock reentrantLock = new ReentrantLock();
    public void sale(){
        reentrantLock.lock();
        try{
            if(number>0){
                number--;
                System.out.println(Thread.currentThread().getName()+"\t"+number);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

}

public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        //Thread.start不会立即执行 等待着操作系统和底层的调度
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"C").start();
    }
}

interface a {
    static String a = "";
    private static void setA(){

    }
    private void o(){

    }
    default void p(){

    }
}
